package com.ayuku.course.ui.employee;

import java.util.Date;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.SqliteDB;
import com.ayuku.commons.Util;
import com.ayuku.commons.ui.ListWithFormMaster;
import com.ayuku.course.persistence.model.Employee;

public class ListPegawai extends ListWithFormMaster {
	private CompEmployee comp;
	private Employee pjo;
	private String name;
	private String password;
	private String level;
	private String phone;
	private String address;
	private String kode;
	private Date joinDate;
	private String email;

	public ListPegawai(Composite parent, int style) {
		super(parent, style);

	}

	@Override
	protected String[] getColumCaption() {
		final String[] titles = { "Kode", "Nama", "Alamat", "Phone", "Level",
				"password", "Join Date" };
		return titles;
	}

	@Override
	protected int[] getColumWidth() {
		int[] i = { 80, 200, 250, 100, 100, 100, 100 };
		return i;
	}

	@Override
	protected void setFormKeyWithSelectedTableItem() {
		TableItem[] selection = getTableData().getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setFormKey(itm.getText(0));
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN, getShell());
		}
	}

	@Override
	public void additionalProcess() {
		System.out.println("... masuk additinal proses ...");
	}

	public static List<Employee> getList(String arg) throws Exception {
		return SqliteDB
				.getPersist()
				.readList(
						Employee.class,
						"SELECT * FROM employee WHERE code LIKE ? or name LIKE ? LIMIT 1000",
						new Object[] { arg, arg });
	}

	@Override
	protected void populateTable(String sqlDml) throws Exception {

		getTableData().removeAll();
		List<Employee> lst = getList(sqlDml);
		getTableData().setItemCount(lst.size());
		setData(new Object[lst.size()][]);

		for (int i = 0; i < lst.size(); i++) {
			Employee pj = lst.get(i);
			getData()[i] = new String[] { Util.nullToString(pj.getCode()),
					Util.nullToString(pj.getName()),
					Util.nullToString(pj.getAddress()),
					Util.nullToString(pj.getPhone()),
					Util.nullToString(pj.getLevel()),
					Util.nullToString(pj.getPassword()),
					Util.dateToString(pj.getJoinDate()) };
		}
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
		item.setText(5, Util.nullToString(obj[5]));
		item.setText(6, Util.nullToString(obj[6]));
	}

	@Override
	public void getCompositeForm(Composite formContainer) {
		comp = new CompEmployee(formContainer, SWT.BORDER);
	}

	@Override
	public void reloadForm() {

		if (pjo != null) {
			comp.getTxtKode().setText(Util.nullToString(pjo.getCode()));
			comp.getTxtName().setText(Util.nullToString(pjo.getName()));
			comp.getTxtAlamat().setText(Util.nullToString(pjo.getAddress()));
			comp.getTxtPhone().setText(Util.nullToString(pjo.getPhone()));
			comp.getTxtEmail().setText(Util.nullToString(pjo.getEmail()));
			comp.getTxtLevel().setText(Util.nullToString(pjo.getLevel()));
			comp.getTxtPassword().setText(Util.nullToString(pjo.getPassword()));
			Util.setDateTimeWithDate(pjo.getJoinDate(), comp.getDateTime());
		}
	}

	@Override
	public void doClearForm(SelectionEvent arg0) {
		pjo = new Employee();
		reloadForm();
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void enabledControl(boolean otherfield, boolean key) {
		comp.getTxtKode().setEditable(key);

	}

	@Override
	public boolean doValidate() {

		kode = comp.getTxtKode().getText();
		if (Util.isEmpty(kode)) {
			Util.showMsgBox("Kode harus dilengkapi!", 1, getShell());
			comp.getTxtKode().setFocus();
			return false;
		}

		name = comp.getTxtName().getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}
		System.out.println("nama: " + name);
		address = comp.getTxtAlamat().getText();
		if (Util.isEmpty(address)) {
			Util.showMsgBox("alamat harus dilengkapi!", 1, getShell());
			comp.getTxtAlamat().setFocus();
			return false;
		}

		phone = comp.getTxtPhone().getText();
		if (Util.isEmpty(phone)) {
			Util.showMsgBox("Phone harus dilengkapi!", 1, getShell());
			comp.getTxtPhone().setFocus();
			return false;
		}

		level = comp.getTxtLevel().getText();
		if (Util.isEmpty(level)) {
			Util.showMsgBox("Level harus dilengkapi!", 1, getShell());
			comp.getTxtLevel().setFocus();
			return false;
		}
		
		email = comp.getTxtEmail().getText();
		if (Util.isEmpty(email)) {
			Util.showMsgBox("Email harus dilengkapi!", 1, getShell());
			comp.getTxtEmail().setFocus();
			return false;
		}

		password = comp.getTxtPassword().getText();
		if (Util.isEmpty(password)) {
			Util.showMsgBox("Password harus dilengkapi!", 1, getShell());
			comp.getTxtPassword().setFocus();
			return false;
		}
		joinDate = Util.getDateSelection(comp.getDateTime());
		return true;
	}

	@Override
	protected void setPojoWithFormField() throws Exception {
		if (pjo == null) {
			pjo = new Employee();
		}
		pjo.setCode(kode);
		pjo.setName(name);
		pjo.setAddress(address);
		pjo.setLevel(level);
		pjo.setPhone(phone);
		pjo.setEmail(email);
		pjo.setPassword(password);
		pjo.setJoinDate(joinDate);
	}

	@Override
	protected void getPojoByFormKey(String key) throws Exception {
		try {
			pjo = (Employee) SqliteDB.getPersist().readByPrimaryKey(
					Employee.class, new Object[] { getFormKey() });
		} catch (Exception e) {

		}
		reloadForm();
	}

	@Override
	protected int doMainUpdate() throws Exception {
		setPojoWithFormField();
		System.out.println("....Masuk update ...");
		return SqliteDB.getPersist().update(pjo);
	}

	@Override
	protected int doMainDelete() throws Exception {
		return delete(pjo.getCode());
	}

	@Override
	protected int delete(String key) throws Exception {
		
		System.out.println("Kode: " +  key);
		return SqliteDB.getPersist().executeUpdate(
				"delete from employee where code = ?",
				new Object[] { key.trim()  });

	}

	@Override
	public void setFocusForAdd() {
		comp.getTxtKode().setFocus();
	}

	@Override
	public void setFocusForEdit() {
		comp.getTxtName().setFocus();
	}

	@Override
	protected int doMainInsert() throws Exception {
		setPojoWithFormField();
		return SqliteDB.getPersist().insert(pjo);
	}

}
